﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AGPR.Locadora.Model;

namespace AGPR.Locadora.Web.Controllers
{
    /// <summary>
    /// Controlador da Sessão
    /// </summary>
    public class SessaoController : BaseController
    {
        #region Index

        public ActionResult Index()
        {
            this.ChecarLogin();

            return RedirectToAction("login");
        }

        #endregion

        protected override ActionResult ChecarLogin()
        {
            if (this.isLogado)
            {
                return RedirectToAction("index", "home");
            }

            return null;
        }

        #region Login

        public ActionResult Login()
        {
            this.ChecarLogin();

            ViewBag.Title = "Entrar no Sistema";

            return View();
        }

        [HttpPost]
        public ActionResult Login(FormCollection login)
        {
            Funcionario usuario = Funcionario.Login(login["login"], login["senha"]);

            if (usuario != null)
            {
                this.isLogado = true;
                this.usuario = usuario;

                return this.ChecarLogin();
            }

            ViewBag.Title = "Entrar no Sistema";

            return this.Index();
        }

        #endregion

        #region Logout

        public ActionResult Logout()
        {
            this.isLogado = false;
            this.usuario = null;

            return this.Index();
        }

        #endregion
    }
}
